<?php
/**
 * @Author: Xu Liu email:liuxu512512@gmail.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-03-14 11:40:15
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
function allowpostbook()
{
    global $_G;
    loadcache('plugin');
    return (int) $_G['uid'] && (
        in_array($_G['groupid'], unserialize($_G['cache']['plugin']['jameson_read']['postbookusergroup']))
        ||
        DB::result_first('SELECT count(uid) FROM %t WHERE status=%d AND uid=%d', array('jamesonread_shenhe', 1, $_G['uid']))
    );
}
function init_jameson($param = 1)
{
    global $_G;
    loadcache('plugin');
    $_G['jameson_read']                      = array();
    $_G['jameson_read']['iskami']            = 0;
    $_G['jameson_read']['trameid']           = $_G['cache']['plugin']['jameson_read']['trame'] ? $_G['cache']['plugin']['jameson_read']['trame'] : 2;
    $_G['jameson_read']['ftjltrameid']           = (int)$_G['cache']['plugin']['jameson_read']['ftjltrameid'];
    $_G['jameson_read']['ftjljine']           = (int)$_G['cache']['plugin']['jameson_read']['ftjljine'];
    $_G['jameson_read']['trametitle']        = $_G['setting']['extcredits'][$_G['jameson_read']['trameid']]['title'];
    $_G['jameson_read']['tramefield']        = 'extcredits' . $_G['jameson_read']['trameid'];
    $_G['jameson_read']['dstrameid']         = $_G['cache']['plugin']['jameson_read']['dstrame'];
    $_G['jameson_read']['dstrametitle']      = $_G['setting']['extcredits'][$_G['jameson_read']['dstrameid']]['title'];
    $_G['jameson_read']['dstramefield']      = 'extcredits' . $_G['jameson_read']['dstrameid'];
    $_G['jameson_read']['fxtrameid']         = $_G['cache']['plugin']['jameson_read']['fxtrame'] ? $_G['cache']['plugin']['jameson_read']['fxtrame'] : 3;
    $_G['jameson_read']['fxtrametitle']      = $_G['setting']['extcredits'][$_G['jameson_read']['fxtrameid']]['title'];
    $_G['jameson_read']['fxtramefield']      = 'extcredits' . $_G['jameson_read']['fxtrameid'];
    $_G['jameson_read']['shoujiurl']         = trim($_G['siteurl'], "/") . "/";
    $_G['jameson_read']['pcchongzhi']        = trim($_G['cache']['plugin']['jameson_read']['pcchongzhi']);
    $_G['jameson_read']['sjchongzhi']        = trim($_G['cache']['plugin']['jameson_read']['sjchongzhi']);
    $_G['jameson_read']['imagereplacecolum'] = 0;
    $_G['jameson_read']['simpleread']        = $_G['cache']['plugin']['jameson_read']['simpleread'];
    $_G['jameson_read']['jamesonread_title'] = $_G['cache']['plugin']['jameson_read']['jamesonread_title'];
    $_G['jameson_read']['seokeywords']       = $_G['cache']['plugin']['jameson_read']['seokeywords'];
    $_G['jameson_read']['seodescr']          = $_G['cache']['plugin']['jameson_read']['seodescr'];
    $_G['jameson_read']['vipgroup']          = unserialize($_G['cache']['plugin']['jameson_read']['vipgroup']);
    $_G['jameson_read']['downgroups']        = unserialize(trim($_G['cache']['plugin']['jameson_read']['downgroups']));
    $_G['jameson_read']['resou']             = explode("\n", trim($_G['cache']['plugin']['jameson_read']['resou']));
    $_G['jameson_read']['authorcentergg']    = $_G['cache']['plugin']['jameson_read']['authorcentergg'];
    $_G['jameson_read']['paihangbang']       = (int) $_G['cache']['plugin']['jameson_read']['paihangbang'];
    $_G['jameson_read']['maxwidth']       = (int) $_G['cache']['plugin']['jameson_read']['maxwidth'];
    $_G['jameson_read']['pcpadding']       = (int) $_G['cache']['plugin']['jameson_read']['pcpadding'];
    $_G['jameson_read']['shownumber']        = $_G['jameson_read']['showfenlei']        = 1;
    $_G['jameson_read']['enableshuping']     = (int) $_G['cache']['plugin']['jameson_read']['enableshuping'];
    $_G['jameson_read']['otherpinglun']      = trim($_G['cache']['plugin']['jameson_read']['otherpinglun']);
    $_G['jameson_read']['createimg']         = 0;
    $_G['jameson_read']['shoujimuban']       = trim($_G['cache']['plugin']['jameson_read']['shoujimubancolor']);
    $_G['jameson_read']['apikey']            = trim($_G['cache']['plugin']['jameson_read']['apikey']);
    $_G['jameson_read']['secretkey']         = trim($_G['cache']['plugin']['jameson_read']['secretkey']);
    $_G['jameson_read']['shuxinglist']       = explode("\n", trim($_G['cache']['plugin']['jameson_read']['shuxinglist']));
    $_G['jameson_read']['dianpingnums']      = (int) $_G['cache']['plugin']['jameson_read']['dianpingnums'];
    $_G['jameson_read']['pindent']      = (int) $_G['cache']['plugin']['jameson_read']['pindent'];
    $_G['jameson_read']['yongjin']           = floatval($_G['cache']['plugin']['jameson_read']['yongjin']);
    $_G['jameson_read']['dsyongjin']         = floatval($_G['cache']['plugin']['jameson_read']['dsyongjin']);
    $_G['jameson_read']['mustlogin']         = (int) $_G['cache']['plugin']['jameson_read']['mustread'];
    $_G['jameson_read']['shoujihuadong']     = 0;
    $_G['jameson_read']['shupinglength']     = (int) $_G['cache']['plugin']['jameson_read']['shupinglength'];
    $_G['jameson_read']['pcyuedugg']         = 1;
    $_G['jameson_read']['shoujiyuedugg']     = 1;
    $_G['jameson_read']['morenfengmian']     = $_G['cache']['plugin']['jameson_read']['morenfengmian'];
    $_G['jameson_read']['fid']               = $_G['cache']['plugin']['jameson_read']['postbookforum'] ? intval($_G['cache']['plugin']['jameson_read']['postbookforum']) : 2;
    C::t('forum_forum')->update($_G['jameson_read']['fid'], array('status' => 0, 'allowhtml' => 1, 'allowbbcode' => 1, 'allowimgcode' => 1, 'allowmediacode' => 1));
    $_G['jameson_read']['jiangli']       = intval($_G['cache']['plugin']['jameson_read']['jiangli']);
    $_G['jameson_read']['enableshupiao'] = intval($_G['cache']['plugin']['jameson_read']['enableshupiao']);
    $_G['jameson_read']['centernav']     = 1;
    $_G['jameson_read']['nocopy']        = intval($_G['cache']['plugin']['jameson_read']['nocopy']);
    $_G['jameson_read']['bootwidth']     = intval($_G['cache']['plugin']['jameson_read']['bootwidth']);
    $_G['jameson_read']['pdfyuansheng']     = intval($_G['cache']['plugin']['jameson_read']['pdfyuansheng']);
    $_G['jameson_read']['pdftools']     = intval($_G['cache']['plugin']['jameson_read']['pdftools']);
    $_G['jameson_read']['pdfright']     = intval($_G['cache']['plugin']['jameson_read']['pdfright']);
    $_G['jameson_read']['fenxiangdaima'] = $_G['cache']['plugin']['jameson_read']['fenxiangdaima'];

    $_G['jameson_read']['qiniuak']      = trim($_G['cache']['plugin']['jameson_read']['qiniuak']);
    $_G['jameson_read']['qiniusk']      = trim($_G['cache']['plugin']['jameson_read']['qiniusk']);
    $_G['jameson_read']['qiniuyuming']      = trim($_G['cache']['plugin']['jameson_read']['qiniuyuming']);
    $_G['jameson_read']['qiniuyuming'] = preg_match("/^http/i",$_G['jameson_read']['qiniuyuming'])?preg_replace("/^https?:\/\//i",'',$_G['jameson_read']['qiniuyuming']):$_G['jameson_read']['qiniuyuming'];;
    $_G['jameson_read']['qiniukongjian']      = $_G['cache']['plugin']['jameson_read']['qiniukongjian'];
    $qiniuquyu = array(
            'huadong'=>'//upload.qiniu.com',
            'huabei'=>'//upload-z1.qiniu.com',
            'huanan'=>'//upload-z2.qiniu.com',
            'beimei'=>'//upload-na0.qiniu.com',
        );
    $_G['jameson_read']['qiniuquyu']      = trim($_G['cache']['plugin']['jameson_read']['qiniuquyu'])?$qiniuquyu[$_G['cache']['plugin']['jameson_read']['qiniuquyu']]:$qiniuquyu['huadong'];

    if (($template = trim(C::t('common_setting')->fetch('jameson_read'))) && is_dir('./source/plugin/jameson_read/template/' . $template)) {
        $_G['jameson_read']['template'] = ($template == 'boot') ? '163' : $template;
    } else {
        $_G['jameson_read']['template'] = '163';
    }
    if ($_G['uid']) {
        $tmp                                     = DB::fetch_first("SELECT *  FROM %t WHERE uid=%d", array('common_member_count', $_G['uid']));
        $_G['jameson_read']['tramenum']          = intval($tmp[$_G['jameson_read']['tramefield']]);
        $_G['jameson_read']['dstramenum']        = $_G['jameson_read']['dstrameid'] ? intval($tmp[$_G['jameson_read']['dstramefield']]) : 0;
        $_G['jameson_read']['fxtramenum']        = intval($tmp[$_G['jameson_read']['fxtramefield']]);
        $_G['jameson_read']['allowpostbook']     = $_G['jameson_read']['is_author']     = allowpostbook();
        $_G['jameson_read']['postbookusergroup'] = unserialize($_G['cache']['plugin']['jameson_read']['postbookusergroup']);
    } else {
        $_G['jameson_read']['tramenum'] = $_G['jameson_read']['dstramenum'] = $_G['jameson_read']['fxtramenum'] = $_G['jameson_read']['allowpostbook'] = $_G['jameson_read']['tramenum'] = $_G['jameson_read']['is_author'] = 0;
    }
}

/**
 *type   category分类  book 图书页面 index首页 author作者中心 thread 阅读页面  fuli福利页面 paihang 排行榜  bookstore 书架
 *
 **/
function jamesonurl($type, $id, $pos = 'index', $fxuid = 0)
{
    global $_G;
    loadcache('plugin');
    $bookpre = $_G['cache']['plugin']['jameson_read']['qianzui'];
    $re      = $_G['cache']['plugin']['jameson_read']['rewurl'];
    $url     = $re ? './' . $bookpre : './plugin.php?id=jameson_read&contrl=';
    switch ($type) {
        case 'category':
        case 'shuping':
        case 'book':
            $url .= $re ? '/' . $type . '/' . $id : 'index&act=' . $type . '&' . $type . '_id=' . $id;
            break;
        case 'index':
            $url .= $re ? '/' : 'index&act=index';
            break;
        case 'author':
            if ($pos == 'book') {
                $url .= $re ? '/author/book/' : 'author&act=book';
                $url .= $re ? ($id ? $id : '') : ($id ? '&book_id=' . $id : '');
            } else if ($pos != 'edit') {
                $url .= $re ? '/author/' . $pos : 'author&act=' . $pos;
            }
            break;
        case 'thread':
            $url .= $re ? '/read/' . $id . '/' . $pos : 'index&act=read&tid=' . $id . '&pid=' . $pos;
            $url .= $re ? ($fxuid ? '/' . $fxuid : '') : ($fxuid ? '&fxuid=' . $fxuid : '');
            break;
        case 'fuli':
        case 'shenqing':
        case 'zhanghu':
        case 'search':
        case 'paihang':
        case 'bookstore':
            $url .= $re ? '/'.$type : 'index&act='.$type;
            break;
        case 'shaixuan':
            if (!$pos) {
                $url .= $re ? '/shaixuan' : 'index&act=shaixuan';
            } else {
                $thisurl = trim(substr($id, strpos($id, 'shaixuan') + 9), '/'); //截取shaixuan后的地址
                $thisurl = trim($thisurl, '&');
                // 获取的参数替换后的shaixuan后的新地址
                $transurl = trim(trim(jamesonshaixuan($thisurl, $pos, $fxuid, $re), '&'), '/');
                $url      = $re ? './' . $bookpre . '/shaixuan/' . $transurl : './plugin.php?id=jameson_read&contrl=index&act=shaixuan&' . $transurl;
            }
            break;
    }
    return $url;
}
/*
    $type对应act，id对应数字，pos对应 add|edit|pid|order
 */
function mbreurl($type, $id=0, $pos = 'index', $fxuid = 0)
{
    global $_G;
    loadcache('plugin');
    $bookpre = $_G['cache']['plugin']['jameson_read']['qianzui'];
    $re      = $_G['cache']['plugin']['jameson_read']['mbrewurl'];
    $url     = $re ? ('./' . $bookpre .'/m'): './plugin.php?id=jameson_read&contrl=';
    switch ($type) {
        case 'category':
        case 'book':
        case 'mulu':
            $url .= $re ? '/' . $type . '/' . $id : 'touch&act=' . $type . '&' . $type . '_id=' . $id;
            break;
        case 'index':
            $url .= $re ? '/' : 'touch&act=index';
            break;
        case 'author':
            if ($pos == 'edit') {
                $url .= $re ? '/author/edit/'.$id : 'touchauthor&act=bookedit&book_id='.$id;
            } else if ($pos == 'add') {
                $url .= $re ? '/author/add/' : 'touchauthor&act=bookadd';
            }else {
                $url .= $re ? '/author' : 'touchauthor&act=index';

            }
            break;
        case 'thread':
            $url .= $re ? '/read/' . $id . '/' . $pos : 'touch&act=read&tid=' . $id . '&pid=' . $pos;
            $url .= $re ? ($fxuid ? '/' . $fxuid : '') : ($fxuid ? '&fxuid=' . $fxuid : '');
            break;
        case 'fuli':
        case 'shenqing':
        case 'store':
        case 'search':
        case 'paihang':
        case 'fenlei':
            $url .= $re ? '/'.$type : 'touch&act='.$type;
            break;
        case 'more':
            $url .= $re ? '/more/'.$pos : 'touch&act=more&type=paihang&order='.$pos;
            break;
    }
    return $url;
}
function jamesonshaixuan($url, $key, $id, $re = 0)
{
    global $_G;
    $url = preg_replace(array("/&p=(\d+)/i", "/\/p\/\d+/i"), array('', ''), $url);
    if (preg_match("/" . $key . "=(\w+)/i", $url)) {
        $rmnull = $id !== 'empty' ? $key . "=" . $id : '';
        $newurl = preg_replace("/" . $key . "=(\w+)/i", $rmnull, $url);
    } else if (preg_match("/" . $key . "\/(\w+)/i", $url)) {
        $rmnull = $id !== 'empty' ? $key . "/" . $id : '';
        $newurl = preg_replace("/" . $key . "\/(\w+)/i", $rmnull, $url);
    } else {
        if ($id === 'empty') {
            $newurl = $url;
        } else {
            $newurl = $re ? ($url . '/' . $key . '/' . $id) : ($url . '&' . $key . '=' . $id);
        }
    }
    $newurl = trim(trim($newurl, '/'), '&');
    $newurl = preg_replace(array('/&{2,}/', '/\/{2,}/'), array('&', '/'), $newurl);
    if ($re) {
        return preg_replace('/&|=/', '/', $newurl);
    } else if (preg_match('/\//', $newurl)) {
        $tmp = '';
        foreach (explode('/', $newurl) as $key => $value) {
            if ($key % 2) {
                $tmp .= $value . '&';
            } else {
                $tmp .= $value . '=';
            }
        }
        return $tmp;
    } else {
        return $newurl;
    }
}

function jamesonread_deletecolum($colum_id)
{
    global $_G;
    if (!$colum_id) {
        return false;
    }
    $tid = DB::fetch_first('SELECT tid,book_id,length FROM %t WHERE colum_id=%d', array('jamesonread_colums', $colum_id));
    $tid && C::t('#jameson_read#jamesonread_colums')->delete($colum_id);
    DB::delete('jamesonread_update',DB::field('tid',$tid));
    $thebook = C::t("#jameson_read#jamesonread_books")->fetch($tid['book_id']);
    if ($thebook) {
        DB::update('jamesonread_books', array(
            'zishu'  => $thebook['zishu'] - $tid['length'],
            'hascol' => $thebook['hascol'] - 1,
        ), DB::field('book_id', $tid['book_id']));
    }
    C::t("forum_thread")->delete($tid['tid']);
    C::t('forum_post')->delete_by_tid('tid:' . $tid['tid'], array($tid['tid']));
    C::t('#jameson_read#jamesonread_colums')->clear_cache(array("mulu".$tid['book_id']));
    C::t("#jameson_read#jamesonread_books")->clear_cache(array('bookinfo-'.$tid['book_id'] . '-0', 'bookinfo-'.$tid['book_id'] . '-' . $_G['uid']));
    return (bool) C::t("forum_thread")->delete($tid['tid']);
}
function jamesonread_deletefenjuan($j_id)
{
    global $_G;
    if (!$j_id) {
        return false;
    }
    C::t("#jameson_read#jamesonread_fenjuan")->delete($j_id);
    $data = DB::fetch_all('SELECT tid,colum_id,book_id,length FROM %t WHERE j_id=%d', array('jamesonread_colums', $j_id));
    if (!$data) {
        return true;
    }
    $zongzishu = 0;
    foreach ($data as $key => $value) {
        DB::delete('jamesonread_update',DB::field('tid',$value['tid']));
        C::t("forum_thread")->delete($value['tid']);
        C::t('forum_post')->delete_by_tid('tid:' . $value['tid'], array($value['tid']));
        $zongzishu += $value['length'];
    }
    DB::delete('jamesonread_colums', DB::field('j_id', $j_id));
    $thebook = C::t("#jameson_read#jamesonread_books")->fetch($data[0]['book_id']);
    if ($thebook) {
        DB::update('jamesonread_books', array(
            'zishu'  => $thebook['zishu'] - $zongzishu,
            'hascol' => $thebook['hascol'] - count($data),
        ), DB::field('book_id', $data[0]['book_id']));
    }
    C::t("#jameson_read#jamesonread_fenjuan")->clear_cache(array('by' . $data[0]['book_id'], 'all' . $data[0]['book_id']));
    C::t("#jameson_read#jamesonread_books")->clear_cache(array('bookinfo-'.$data[0]['book_id'] . '-0', 'bookinfo-'.$data[0]['book_id'] . '-' . $_G['uid']));
    C::t('#jameson_read#jamesonread_colums')->clear_cache(array("mulu".$data[0]['book_id']));
    return true;
}
function deleteqiniupdf($pdf)
{
    global $_G;
    if($_G['jameson_read']['qiniuak'] && $_G['jameson_read']['qiniusk'] && $_G['jameson_read']['qiniuyuming'] && $_G['jameson_read']['qiniukongjian']){
        if (preg_match("/" . $_G['jameson_read']['qiniuyuming'] . "/i", $pdf['url'])) {
            // 删除七牛
            require_once  './source/plugin/jameson_read/class/autoload.php';
            $auth = new Auth($_G['jameson_read']['qiniuak'], $_G['jameson_read']['qiniusk']);
             //初始化BucketManager
            $bucketMgr = new BucketManager($auth);
            $pos    = preg_match("/^http/i", $pdf['url']) ? 8 : 2;
            $err = $bucketMgr->delete($_G['jameson_read']['qiniukongjian'], substr($pdf['url'],stripos($pdf['url'],'/',$pos)+1));
        }
    }
}
function jamesonread_deletebook($book_id)
{
    global $_G;
    if (!$book_id) {
        return false;
    }
    $bookinfo = C::t('#jameson_read#jamesonread_books')->fetch($book_id);
    C::t('#jameson_read#jamesonread_books')->delete($book_id);
    if($bookinfo['is_pdf']){
        $pdfdata = DB::fetch_all('SELECT url FROM %t WHERE book_id=%d',array('jamesonread_pdf',$book_id));
        DB::delete('jamesonread_pdf', DB::field('book_id', $book_id));
        foreach ($pdfdata as $key => $value) {
            if(!preg_match('/^http|\/\//i',$value['url'])){
                @unlink($value['url']);
            }else{
                deleteqiniupdf($value);
            }
        }
        return true;
    }
    DB::delete('jamesonread_fenjuan', DB::field('book_id', $book_id));
    DB::delete('jamesonread_update',DB::field('book_id',$book_id));
    $data = DB::fetch_all('SELECT tid FROM %t WHERE book_id=%d', array('jamesonread_colums', $book_id));
    foreach ($data as $key => $value) {
        DB::delete('forum_thread', DB::field('tid', $value['tid']));
        C::t('forum_post')->delete_by_tid('tid:' . $value['tid'], array($value['tid']));
    }
    DB::delete('jamesonread_colums', DB::field('book_id', $book_id));
    C::t('#jameson_read#jamesonread_books')->clear_cache(array('bookinfo-'.$book_id . '-' . $_G['uid'], 'bookinfo-'.$book_id . "-0"));
    C::t('#jameson_read#jamesonread_books')->clear_cache(array('jamesonreadnewtuijian', 'jamesonreadnewpaihang'));
    return true;
}
function jamesonread_deletepdf($id)
{
    global $_G;
    if (!$id) {
        return false;
    }
    $pdf = C::t('#jameson_read#jamesonread_pdf')->fetch($id);
    if(!preg_match('/^http|\/\//i',$pdf['url'])){
        @unlink($pdf['url']);
    }else{
        deleteqiniupdf($pdf);
    }
    C::t('#jameson_read#jamesonread_pdf')->delete($id);
    return true;
}
// 系统提醒 购买图书 购买章节 评论图书 打赏
function jameson_sendsms($uid, $authorid, $author, $note, $book_id)
{
    global $_G;
    C::t('home_notification')->insert(array(
        'uid'         => $uid,
        'new'         => 1,
        'type'        => 'system',
        'authorid'    => $authorid,
        'author'      => $author,
        'note'        => $note,
        'dateline'    => TIMESTAMP,
        'from_id'     => $book_id,
        'from_idtype' => 'book',
        'from_num'    => 1,
        'category'    => 3,
    ));
    if ($oldprompt = unserialize(DB::result_first('SELECT data FROM %t WHERE uid=%d', array('common_member_newprompt', $uid)))) {
        if ($oldprompt['system']) {
            $oldprompt['system'] = intval($oldprompt['system']) + 1;
        } else {
            $oldprompt['system'] = 1;
        }
        C::t('common_member_newprompt')->update($uid, array('data' => serialize($oldprompt)));
    } else {
        C::t('common_member_newprompt')->insert($uid, array('system' => 1));
    }
    $oldnum = (int) DB::result_first('SELECT newprompt FROM %t WHERE uid=%d', array('common_member', $uid));
    DB::update('common_member', array('newprompt' => 1 + $oldnum), "uid='$uid'");
}

function xfxs_cnleng($str,$encoding="utf8")
{

    if (($len = strlen($str)) == 0) {
        return 0;
    }

    $encoding = strtolower($encoding);

    if ($encoding == "utf8" or $encoding == "utf-8") {
        $step = 3;
    } elseif ($encoding == "gbk" or $encoding == "gb2312") {
        $step = 2;
    } else {
        return false;
    }

    $count = 0;
    for ($i=0; $i<$len; $i++) {
        $count++;
        //如果字节码大于127，则根据编码跳几个字节
        if (ord($str{$i}) >= 0x80) {
            $i = $i + $step - 1;//之所以减去1，因为for循环本身还要$i++
        }
    }
    return $count;
}
function xfxs_isutf($str)
{
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        $c = ord($str[$i]);
        if ($c > 128) {
            if (($c > 247)) {
                return false;
            } elseif ($c > 239) {
                $bytes = 4;
            } elseif ($c > 223) {
                $bytes = 3;
            } elseif ($c > 191) {
                $bytes = 2;
            } else {
                return false;
            }
            if (($i + $bytes) > $len) {
                return false;
            }
            while ($bytes > 1) {
                $i++;
                $b = ord($str[$i]);
                if ($b < 128 || $b > 191) {
                    return false;
                }
                $bytes--;
            }
        }
    }
    return true;
}
function fxact($fxuid, $tid, $book_id, $jiangli, $trameid, $ip)
{
    $fxuid   = intval($fxuid);
    $tid     = intval($tid);
    $book_id = intval($book_id);
    $jiangli = intval($jiangli);
    $trameid = intval($trameid);
    $ip      = daddslashes($ip);
    // 如果不曾存在此fxuid，tid，ip
    if (!DB::result_first("SELECT count(*) FROM %t WHERE fxuid=%d AND tid=%d AND ip='" . $ip . "'", array('jamesonread_fx', $fxuid, $tid, $ip))) {
        // 奖励积分
        if (!updatemembercount($fxuid,
            array($trameid => $jiangli),
            true, 'jmd', 0, lang('plugin/jameson_read', 'fenxiangjiangli'))) {
            // 奖励积分成功，插入fx表和积分日志
            C::t('#jameson_read#jamesonread_fx')->insert(array(
                'fxuid'   => $fxuid,
                'addtime' => TIMESTAMP,
                'tid'     => $tid,
                'book_id' => $book_id,
                'ip'      => $ip,
                'jiangli' => $jiangli,
            ));
            return true;
        }
    }
    return false;
}

function parsearray($a, $pre = '')
{
    if ($pre) {
        $pre = substr($pre, strpos($pre, '::') + 2);
    }
    if (!is_array($a)) {
        $a   = (string) $a;
        $ret = md5($pre . $a);
    } else {
        $tmp = '';
        foreach ($a as $key => $value) {
            if (!is_array($value)) {
                $value = (string) $value;
                $tmp .= $key . '-' . $value . '_';
            } else {
                $value = parsearray($value);
                $tmp .= $value;
            }
        }
        $ret = md5($pre . $tmp);
    }
    if (strlen($ret) > 25) {
        return substr($ret, 0, 15) . substr($ret, -5);
    } else {
        return $ret;
    }
}